package kalender;
 
import java.io.*;
import java.net.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
public class EchoClient {
    
    private int port;
    private String serverName;    
    private Socket echoSocket = null;
    private PrintWriter out = null;
    private BufferedReader in = null;
 
    public EchoClient(String serverName, int port) throws Exception {
        this.port = port;
        this.serverName = serverName;
        connect();
    }
    
    private void connect() {
        try {
            echoSocket = new Socket(serverName, port);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + serverName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                               + "the connection to: " + serverName);
            System.exit(1);
        }
    }
    
    public void close() throws IOException {
        out.close();
        in.close();
        echoSocket.close();
    }
    public ArrayList<Person> getPersonList() throws IOException {
        String input;
        ArrayList<String> enPerson = new ArrayList<String>();
        ArrayList<Person> allePersoner = new ArrayList<Person>();
        out.println("getPersonList");
        input = in.readLine();
        while(!input.equals("finished")) {
            enPerson.add(input);
            if(input.equals("*")) {
                allePersoner.add(new Person(Integer.parseInt(enPerson.get(0)),enPerson.get(1),enPerson.get(2),enPerson.get(3),enPerson.get(4)));
                enPerson.clear();
            }
            input = in.readLine();
        }
        return allePersoner;
    }
    
    public ArrayList<Rom> getRoomList() throws IOException {
        String input;
        ArrayList<String> aRoom = new ArrayList<String>();
        ArrayList<Rom> allRooms = new ArrayList<Rom>();
        out.println("getRoomList");
        input = in.readLine();
        while(!input.equals("finished")) {
            aRoom.add(input);
            if(input.equals("*")) {
                allRooms.add(new Rom(Integer.parseInt(aRoom.get(0)),aRoom.get(1),Integer.parseInt(aRoom.get(2))));
                aRoom.clear();
            }
            input = in.readLine();
        }
        return allRooms;
    }
    
    public void getOccupiedList() throws IOException {
        String input;
        ArrayList<String> aRoom = new ArrayList<String>();
        out.println("getOccupiedList");
        input = in.readLine();
        while(!input.equals("finished")) {
            aRoom.add(input);
            if(input.equals("*")) {
            	if(Hovedklasse.getRomliste().contains(Hovedklasse.getRomWithID(Integer.parseInt(aRoom.get(0))))){
            		Hovedklasse.getRomWithID(Integer.parseInt(aRoom.get(0))).setOpptatt(aRoom.get(1));
            	}
            }
            input = in.readLine();
        }
        
    }
    
    public ArrayList<Avtale> getAppointmentList() throws IOException, NumberFormatException, ClassNotFoundException, SQLException {
        String input;
        ArrayList<String> anAppointment = new ArrayList<String>();
        ArrayList<Avtale> allAppointments = new ArrayList<Avtale>();
        ArrayList<Person> deltakere = new ArrayList<Person>();
        out.println("getAppointmentList");
        input = in.readLine();
        while(!input.equals("finished")) {
            anAppointment.add(input);
            if(input.equals("*")) {
                if(anAppointment.size() > 9){
                    for(int i = 10; i < anAppointment.size()-1; i++){
                        deltakere.add(Hovedklasse.getPersonWithID(Integer.parseInt(anAppointment.get(i))));
                    }
                    Mote mote = new Mote(Integer.parseInt(anAppointment.get(0)), anAppointment.get(1),Integer.parseInt(anAppointment.get(2)),anAppointment.get(3),anAppointment.get(4),anAppointment.get(5), deltakere,Integer.parseInt(anAppointment.get(6)), false);
                    allAppointments.add(mote);
                    for(Person p: deltakere){
                        p.getKalender().leggTilAvtale(mote);
                    }
                }
                else if(Boolean.parseBoolean(anAppointment.get(7)))
                	allAppointments.add(new Mote(Integer.parseInt(anAppointment.get(0)), anAppointment.get(1),Integer.parseInt(anAppointment.get(2)),anAppointment.get(3),anAppointment.get(4),anAppointment.get(5), deltakere,Integer.parseInt(anAppointment.get(6)), false));
                else{
                    allAppointments.add(new Avtale(Integer.parseInt(anAppointment.get(0)), anAppointment.get(1),Integer.parseInt(anAppointment.get(2)),anAppointment.get(3),anAppointment.get(4),anAppointment.get(5),Integer.parseInt(anAppointment.get(6)), false));
                }
                anAppointment.clear();
            }
            input = in.readLine();
        }
        return allAppointments;
    }
    
    public ArrayList<Melding> getMessageList() throws IOException{
        String input;
        ArrayList<String> aMessage = new ArrayList<String>();
        ArrayList<Melding> allMessages = new ArrayList<Melding>();
        
        out.println("getMessageList");
        input = in.readLine();
        while(!input.equals("finished")){
            aMessage.add(input);
            if(input.equals("*")) {
                allMessages.add(new Melding(Hovedklasse.getPersonWithID(Integer.parseInt(aMessage.get(0))), aMessage.get(1), Boolean.parseBoolean(aMessage.get(2)), Hovedklasse.getMote(Integer.parseInt(aMessage.get(3)))));
                aMessage.clear();
            }
            input = in.readLine();
        }
        
        
        return allMessages;
    }
    
    public boolean checkPassword(String brukernavn, String passord) throws IOException{
        String input;
        out.println("checkPassword" + "-" + brukernavn + "-" + passord);
        input = in.readLine();
        return input.equals("true") ? true: false;
        
    }
    
    public void executeUpdate(String query){
        
            out.println("updateComing");
            out.println(query);
            out.println("finished");
        
    }
    
    public int getLastId() throws NumberFormatException, IOException{
    	out.println("getLastId");
    	return Integer.parseInt(in.readLine());
    }
    
    public void executeDelAndIns(ArrayList<String> queries){
        for(String query : queries){
            out.println("IorDComing");
            out.println(query);
            out.println("finished");
        }
    }
    
    
}